//
//  Constants.swift
//  HeadlineNews
//
//  Created by han on 2019/6/28.
//  Copyright © 2019 han. All rights reserved.
//

import UIKit

// MARK: 判断当前设备
struct DevicePlatform {
    
    static private var isDeviceIPad = -1
    static func isIPad() -> Bool {
        if isDeviceIPad < 0 {
            isDeviceIPad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad ? 1: 0
        }
        return isDeviceIPad > 0
    }
    
    static private var isDeviceIPod = -1
    static func isIPod() -> Bool {
        if isDeviceIPod < 0 {
            let string = UIDevice.current.model
            isDeviceIPod =  (string.range(of: "iPod touch") != nil) ? 1: 0
        }
        return isDeviceIPod > 0
    }
    
    static private var isDeviceIPhone = -1
    static func isIPhone() -> Bool {
        if isDeviceIPhone < 0 {
            let string = UIDevice.current.model
            isDeviceIPhone =  (string.range(of: "iPhone") != nil) ? 1: 0
        }
        return isDeviceIPhone > 0
    }
    
    static private var isDeviceSimulator = -1
    static func isSimulator() -> Bool {
        if isDeviceSimulator < 0 {
            #if targetEnvironment(simulator)
            isDeviceSimulator = 1;
            #else
            isDeviceSimulator = 0;
            #endif
        }
        return isDeviceSimulator > 0
    }
    
    static var isRetina: Bool {
        return UIScreen.main.scale >= 2.0
    }
    
    // 判断当前设备是否为放大模式
    static func isZoomMode() -> Bool {
        if !DevicePlatform.isIPhone() {
            return false
        }
        // 手机分辨率分为，逻辑分辨率，屏幕渲染分辨率，物理分辨率。
        //放大模式下逻辑分辨率和屏幕渲染分辨率会降低。
        let nativeScale = UIScreen.main.nativeScale // 物理分辨率/逻辑分辨率
        var scale = UIScreen.main.scale // 设备渲染分辨率/逻辑分辨率
        
        // 标准情况下 7：scale:2.0 nativeScale:2.0  ：375x667，750x1334
        // 放大模式下 7：scale:2.0 nativeScale:2.34375 ：放大模式后变为 320x568，640x1136 但是实际的物理分辨率仍旧 750x1334，所以nativeScale变为 1334/568
        // 通过比较这两个比例来判断是否为放大模式
        
        // 对于所有的 Plus 系列 iPhone，屏幕物理像素低于软件层面的渲染像素，不管标准模式还是放大模式，nativeScale 均小于 scale，所以需要特殊处理才能准确区分放大模式
        let shouldBeDownsampledDevice = UIScreen.main.nativeBounds.size.equalTo(CGSize(width: 1080, height: 1920))
        // Plus系列的实际像素1920x1080，实际缩放为2.46
        if (shouldBeDownsampledDevice) {
            scale /= 1.15;
        }
        return nativeScale > scale
    }
}

// MARK:  计算尺寸大小
struct CommonSize {
    // 是否横屏
    static let isLandscape = UIApplication.shared.statusBarOrientation.isLandscape
    // 设备是否是横屏了，与软件是否横屏无关
    static let isDeviceLandscape = UIDevice.current.orientation.isLandscape
    
    // screen 这个屏幕尺寸会根据横屏竖屏而发生改变。
    static let screenHeight = UIScreen.main.bounds.height
    static let screenWidth = UIScreen.main.bounds.width
    static let screenSize = UIScreen.main.bounds.size
    static let screenBounds = UIScreen.main.bounds
    static let screenScale = UIScreen.main.scale
    
    // device 固定的设备尺寸
    static let deviceHeight = CommonSize.isLandscape ? CommonSize.screenWidth: CommonSize.screenHeight
    static let deviceWidth = CommonSize.isLandscape ? CommonSize.screenHeight: CommonSize.screenWidth
    
    // 隐藏/显示状态栏，来电，地图导航在后台时，高度会发生改变，所以实时调用
    static let statusBarHeight = UIApplication.shared.isStatusBarHidden ? 0:  UIApplication.shared.statusBarFrame.height
   

    // safeArea
    static let windowSafeAreaInset = { () -> UIEdgeInsets in
        if #available(iOS 11.0, *) {
            return UIApplication.shared.keyWindow?.safeAreaInsets ?? UIEdgeInsets.zero
        }
        return UIEdgeInsets.zero
    }
    
    static let windowSafeAreaTop = CommonSize.windowSafeAreaInset().top
    // 34 or 0
    static let windowSafeAreaBottom = CommonSize.windowSafeAreaInset().bottom
    
    
}

//  view size
extension UINavigationController {
    var barHeight: CGFloat {
        return navigationBar.frame.size.height
    }
    
    var barContentTop: CGFloat {
        return barHeight + CommonSize.statusBarHeight
    }
}

extension UITabBarController {
    var tabBarHeight: CGFloat {
        return tabBar.frame.height
    }
}

extension UIView {
    
    // safeAreaInset
    func hd_safeAreaInset() -> UIEdgeInsets {
        if #available(iOS 11.0, *) {
            return self.safeAreaInsets
        }
        return UIEdgeInsets.zero
    }
}

//  MARK: 获取版本号
struct HeadlineVersion {
    static let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString")
    static let versionBuild = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion")
}

//  MARK: -
struct CommonColor {
    let titleColor = UIColor(hex6: 0x333333)
    let subtitleColor = UIColor(hex6: 0x666666)
    let lightTitleColor = UIColor(hex6: 0x999999)
    let mainThemeColor = UIColor(hex6: 0xEA233D)
    let lineColor = UIColor(hex6: 0xebebeb)
    let backgroundColor = UIColor(hex6: 0xf7f7f7)
    
}

//  MARK: -
struct CommonPadding {

    let horizenMargin = CGFloat(10)
    let verticalMargin = CGFloat(15)
    let horizenPadding = CGFloat(8)
    let verticalPadding = CGFloat(12)
    
}


struct FileURL {
  static let cachesURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
  static let libraryURL = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask)[0]
  
}
